﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCL.Powersource;
using UCL.Powersource.Helpers;

namespace ToolkitTestApp
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    [Export(typeof(MainView))]
    public partial class MainView : UserControl
    {

        public MainViewModel VM { get { return (MainViewModel)DataContext; } }
        public MainView()
        {
            InitializeComponent();
            Loaded += MainView_Loaded;
        }

        void MainView_Loaded(object sender, RoutedEventArgs e)
        {
            lbTestSelected.SelectionChanged += lbTestSelected_SelectionChanged;
            VM.AvailableTeams[2].IsSelected = true;
            VM.AvailableTeams[4].IsSelected = true;
            //var r = lbTestSelected.SelectedItemsCollection as ObservableCollection<MainViewModel.LBItem>;
            //var r1 = lbTestSelected.SelectedItemsCollection as INotifyCollectionChanged;
            //r1.CollectionChanged += r1_CollectionChanged;
        }

        private ILog Log = LogManager.GetLog(null);

        //void r1_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        //{
        //    Log.DebugMethod("Action={0}, NewItems={1}, OldItems={2}", e.Action, e.NewItems, e.OldItems);
        //    if (e.NewItems != null)
        //    {
        //        List <string> lsLogMsg=new List<string>();
        //        foreach (var it in e.NewItems)
        //        {
                    
        //            var r = it as SelectableConfigurationItem<MainViewModel.LBItem>;
        //            if (r == null)
        //            {
        //                lsLogMsg.Add(string.Format("Item {0} - {1}",it.GetType().Name,it.ToString()));
        //            }
        //            else
        //            {
        //                var itemToConfigureString="#NULL";
        //                if (r.ItemToConfigure!=null) itemToConfigureString=r.ItemToConfigure.Name;
        //                lsLogMsg.Add(string.Format("LBItem {0} - {1} [{2}]",r.Label,r.IsSelected,itemToConfigureString));
        //            }
        //        }
        //        Log.DebugMethod("NewItems: {0}",string.Join(", ", lsLogMsg));
        //    }
        //}

        void lbTestSelected_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void AddOneSelected_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var it in lbTestSelected.ItemsSource)
            {
                if (lbTestSelected.SelectedItems.Contains(it))
                {
                    continue;
                }
                else
                {
                    lbTestSelected.SelectedItems.Add(it);
                    break;
                }
            }


        }

        private void ChangeOneIsSelected_Click_1(object sender, RoutedEventArgs e)
        {
            VM.AvailableTeams[2].IsSelected = !VM.AvailableTeams[2].IsSelected;
            //foreach (var it in lbTestSelected.ItemsSource.Cast<SelectableConfigurationItem<MainViewModel.LBItem>>())
            //{
            //    if (it.IsSelected)     
            //}

        }

        private void ChangeAllIsSelected_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (var it in lbTestSelected.ItemsSource.Cast<SelectableConfigurationItem<MainViewModel.LBItem>>())
            {
                it.IsSelected = !it.IsSelected;
            }
        }
    }
}
